const query = require('../../dbConfig/index') 
const get = async (ctx,next)=>{
	const body = ctx.request.body
	const offset = (body.pageNumber - 1) * Number(body.pageSize);  
	let sql = 'SELECT * FROM teacher';  
	let params = [];  
	// 如果传了条件 就拼接sql
	if (body.teacher_name) {  
		sql += sql.includes('WHERE')? ' and teacher_name=?': ' WHERE teacher_name=?';  
		params.push(body.teacher_name);  
	}
	if (body.teacher_sex) {  
		sql += sql.includes('WHERE')?  'and teacher_sex=?':' WHERE teacher_sex=?';  
		params.push(body.teacher_sex);  
	}
	if (body.speciality) {  
		sql += sql.includes('WHERE')?  'and speciality=?':' WHERE speciality=?';  
		params.push(body.speciality);  
	}
	if(ctx.state.power!=1&&ctx.state.classes){
		let str = ctx.state.classes.split(",").map(num => parseInt(num))
		sql += sql.includes('WHERE')? ' and classes IN (?)':' WHERE classes IN (?)';  
		params.push(str); 
	}
	if (body.teacher_subject) {  
		sql +=sql.includes('WHERE')? ' and teacher_subject like ?': ' WHERE teacher_subject like ?';  
		params.push('%'+body.teacher_subject+'%');  
	}
	 
	  sql += ' LIMIT ? OFFSET ?';  
	  params.push(Number(body.pageSize));  
	  params.push(offset);
	const res = await query(sql,params)
	const total = await query(`SELECT COUNT(*) as total FROM teacher`)
	if(res.length>0){
		ctx.body={code:200,data:{records:res,total:total[0].total},msg:'成功'}
		next()
	}else{
		ctx.body={code:0,data:null,msg:'没有教师数据'}
	}
}
const getItem = async (ctx,next)=>{
	const body = ctx.request.body
	let sql = `SELECT t.* FROM student s LEFT JOIN teacher t ON t.classes LIKE CONCAT('%', s.classes_id, '%') where s.student_id=${body.student_id}` 
	const res = await query(sql)
	if(res.length>0){
		ctx.body={code:200,data:res,msg:'成功'}
		next()
	}else{
		ctx.body={code:0,data:null,msg:'没有任课老师数据'}
	}
}
const updata = async (ctx,next)=>{
	const body = ctx.request.body
	await query(`UPDATE teacher SET teacher_username = ?,teacher_name=?,teacher_sex=?,teacher_age=?,teacher_subject=?,power = ?,teacher_introduction=?,teacher_tel=?,classes=?,speciality=?,avatar_icon=? WHERE teacher_id='${body.teacher_id}'`,[body.teacher_username,body.teacher_name,body.teacher_sex,body.teacher_age,body.teacher_subject,body.power,body.teacher_introduction,body.teacher_tel,body.classes,body.speciality,body.avatar_icon])
	ctx.body={code:200,data:null,msg:'成功'}
	next()
	
}
const add = async (ctx,next)=>{
	const body = ctx.request.body
	await query(`INSERT INTO teacher (teacher_username,teacher_password,teacher_name,teacher_sex,teacher_age,teacher_subject
		,power,teacher_introduction,teacher_tel,classes,speciality,avatar_icon) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)`,[body.teacher_username,'123456',body.teacher_name,body.teacher_sex,body.teacher_age,body.teacher_subject,body.power,body.teacher_introduction,body.teacher_tel,body.classes,body.speciality,body.avatar_icon])
	ctx.body={code:200,data:null,msg:'成功'}
	next()
}
const remove = async (ctx,next)=>{
	const body = ctx.request.body
	await query(`DELETE FROM teacher WHERE teacher_id=${body.id}`)
	ctx.body={code:200,data:null,msg:'成功'}
	next()
}
const rePass = async (ctx,next)=>{
	const body = ctx.request.body
	console.log(body)
	await query(`UPDATE teacher SET teacher_password = ? WHERE teacher_id='${body.id}'`,['123456'])
	ctx.body={code:200,data:null,msg:'成功'}
	next()
}

module.exports = {
	get,getItem,
	add,updata,remove,rePass
}